﻿using System;
using System.Data;
using System.Data.OleDb;
using System.Web.UI.WebControls;
using TraditionalMalteseCuisine.App_Code.Functions;
using TraditionalMalteseCuisine.App_Code.Objects;
using TraditionalMalteseCuisine.App_Code.Operations;

namespace TraditionalMalteseCuisine
{
    public partial class Recipes : System.Web.UI.Page
    {
        #region Class Members
        private int mPageSize = 10;
        #endregion


        #region Page Load
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if ((SearchTerm == null) || (SearchTerm.Length == 0))
                {
                    if ((LevelID != null) && (LevelID.Length > 0) && (Validation_Functions.IsNumeric(LevelID)))
                    {
                        lblMainTitle.Text = string.Format("Recipe(s) marked as '{0}' level", Site_Functions.GetLevelByID(int.Parse(LevelID)));

                        BindData(Recipe.GetCommand_GetRecordsBy(Recipe.VIEW_NAME_ALL_RECIPES, Recipe.LEVEL_ID_TAG, LevelID));
                    }
                    else
                    {
                        if ((TagID != null) && (TagID.Length > 0) && (Validation_Functions.IsNumeric(TagID)))
                        {
                            lblMainTitle.Text = string.Format("Recipe(s) under '{0}' tag", Site_Functions.GetTagByID(int.Parse(TagID)));

                            BindData(Recipe.GetCommand_GetRecordsBy(Recipe.VIEW_NAME_ALL_RECIPES_DETAILS, RecipeTag.TAG_ID_TAG, TagID));
                        }
                        else
                        {
                            if ((MemberID != null) && (MemberID.Length > 0) && (Validation_Functions.IsNumeric(MemberID)))
                            {
                                lblMainTitle.Text = string.Format("Recipe(s) by {0}", Site_Functions.GetSiteMemberByID(int.Parse(MemberID)));

                                BindData(Recipe.GetCommand_GetRecordsBy(Recipe.VIEW_NAME_ALL_RECIPES, Recipe.MEMBER_ID_TAG, MemberID));
                            }
                            else
                            {
                                lblMainTitle.Text = "All Recipe(s)";

                                BindData(Recipe.GetCommand_GetAllRecords(Recipe.VIEW_NAME_ALL_RECIPES, Recipe.POSTED_DATE_TAG));
                            }
                        }
                    }
                }
                else
                {
                    lblMainTitle.Text = string.Format("Result(s) for '{0}'", SearchTerm);

                    BindData(Recipe.GetCommand_SearchByRecipeName(SearchTerm));
                }
            }
        }
        #endregion


        #region Methods
        private void BindData(OleDbCommand oleDbCommand)
        {
            try
            {
                DataTable dtRecipes = ReadOp.GetDataTable(oleDbCommand);

                if ((dtRecipes != null) && (dtRecipes.Rows.Count > 0))
                {
                    PagedDataSource dsPages = new PagedDataSource();
                    dsPages.DataSource = dtRecipes.DefaultView;
                    dsPages.AllowPaging = true;
                    dsPages.PageSize = mPageSize;

                    double totalPages = (dtRecipes.Rows.Count) / dsPages.PageSize;
                    totalPages = Math.Ceiling(totalPages) + 1;

                    int currentPage = 1;
                    if ((Request.QueryString["page"] != null) && (!Request.QueryString["page"].Equals(string.Empty)))
                        currentPage = Convert.ToInt32(Request.QueryString["page"]);

                    dsPages.CurrentPageIndex = (currentPage - 1);
                    lblTopCurrentPage.Text = string.Format("{0} of {1}", currentPage, totalPages);
                    lblBottomCurrentPage.Text = lblTopCurrentPage.Text;

                    if (!dsPages.IsFirstPage)
                    {
                        lnkTopPrevious.NavigateUrl = string.Format("{0}?page={1}", Request.CurrentExecutionFilePath, (currentPage - 1));
                        lnkBottomPrevious.NavigateUrl = lnkTopPrevious.NavigateUrl;
                    }
                    else
                    {
                        lnkTopPrevious.Visible = false;
                        lnkBottomPrevious.Visible = lnkTopPrevious.Visible;
                    }

                    if (!dsPages.IsLastPage)
                    {
                        lnkTopNext.NavigateUrl = string.Format("{0}?page={1}", Request.CurrentExecutionFilePath, (currentPage + 1));
                        lnkBottomNext.NavigateUrl = lnkTopNext.NavigateUrl;
                    }
                    else
                    {
                        lnkTopNext.Visible = false;
                        lnkBottomNext.Visible = lnkTopNext.Visible;
                    }

                    rptRecipies.DataSource = dsPages;
                    rptRecipies.DataBind();
                }
                else
                {
                    control_wrapper.Visible = false;
                }

                lblMessage.Visible = (!control_wrapper.Visible);
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
        #endregion


        #region Properties
        protected string SearchTerm
        {
            get { return Request.Params["search"]; }
        }

        protected string LevelID
        {
            get { return Request.Params["level"]; }
        }

        protected string TagID
        {
            get { return Request.Params["tag"]; }
        }

        protected string MemberID
        {
            get { return Request.Params["user"]; }
        }
        #endregion
    }
}